#include "kurk.h"
#include "ui_kurk.h"

Kurk::Kurk(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Kurk)
{
    ui->setupUi(this);
}

Kurk::~Kurk()
{
    delete ui;
}

void Kurk::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Kurk::on_connectBtn_clicked()
{
    QString port;
    port = ui->portCmb->currentText();

    if(comm.connect(port))
    {
        ui->statusLbl->setText("Success");
        arduino.comm(&comm);
        ui->distBtn->setEnabled(true);
        ui->forwardBtn->setEnabled(true);
        ui->leftBtn->setEnabled(true);
        ui->reverseBtn->setEnabled(true);
        ui->rightBtn->setEnabled(true);
        ui->stopBtn->setEnabled(true);
        ui->angleSpin->setEnabled(true);
        ui->serialOutTxt->setEnabled(true);
        ui->testSerialBtn->setEnabled(true);
        ui->turretSlider->setEnabled(true);
        ui->btnGetVals->setEnabled(true);
        ui->btnLoadVals->setEnabled(true);
        ui->btnSaveVals->setEnabled(true);
        ui->btnSetVals->setEnabled(true);
        ui->btnStop->setEnabled(true);
        ui->btnStart->setEnabled(true);
    }
    else
    {
        ui->statusLbl->setText("Failure");
    }
}

void Kurk::on_distBtn_clicked()
{
    QString frontLeftDist, frontRightDist, backLeftDist, backRightDist;

    frontLeftDist.setNum(arduino.getDistance('L', 'F'));
    backLeftDist.setNum(arduino.getDistance('L', 'B'));
    frontRightDist.setNum(arduino.getDistance('R', 'F'));
    backRightDist.setNum(arduino.getDistance('R', 'B'));

    ui->frontLeftDistLbl->setText(frontLeftDist);
    ui->frontRightDistLbl->setText(frontRightDist);
    ui->backLeftDistLbl->setText(backLeftDist);
    ui->backRightDistLbl->setText(backRightDist);
}

void Kurk::on_forwardBtn_clicked()
{
    arduino.move('F');
}

void Kurk::on_reverseBtn_clicked()
{
    arduino.move('B');
}

void Kurk::on_stopBtn_clicked()
{
    arduino.move('S');
}

void Kurk::on_leftBtn_clicked()
{
    arduino.spin('L');
}

void Kurk::on_rightBtn_clicked()
{
    arduino.spin('R');
}

void Kurk::on_btnLoadImage_clicked()
{
//    QString fileName;
//    fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "%HOMEPATH%", tr("Images (*.jpg)"));
//    if(fileName != NULL)
//    {
//        cam.findStripe(fileName);
//    }
    std::cout << cam.findStripe() << std::endl;
}

void Kurk::on_getImageBtn_clicked()
{
    imgNum = ui->imageTxt->text().toInt();

    cam.showImage();

    imgNum++;

    ui->imageTxt->setText(QString::number(imgNum));
}

void Kurk::on_connectCamBtn_clicked()
{
    if(cam.connectCam(0))
    {
        ui->getImageBtn->setEnabled(true);
        ui->getVideoBtn->setEnabled(true);
        vidIn.connectCam(&cam);
    }
}

void Kurk::on_angleSpin_valueChanged()
{
    //alert.setText(ui->angleSpin->text());
    //alert.exec();

    ui->serialOutTxt->appendPlainText(arduino.turret(ui->angleSpin->text().toInt()) + "\n");

}

void Kurk::on_testSerialBtn_clicked()
{
    for(int i = 600, j = 0; i < 2600; i++, j++)
    {
        ui->serialOutTxt->appendPlainText(QString::number(j) + ": ");
        ui->serialOutTxt->appendPlainText(arduino.turret(i) + "\n");
        //QTest::qWait(10);
        //QTestEventList::addDelay(10);
    }
}

void Kurk::on_getVideoBtn_clicked()
{
    vidIn.start();
    //vidIn.wait();
}

void Kurk::on_turretSlider_sliderMoved()
{
    moveTurret();
}

void Kurk::on_turretSlider_valueChanged()
{
    moveTurret();
}

void Kurk::moveTurret()
{
    ui->angleLbl->setText(QString::number(ui->turretSlider->value()));
    arduino.turret(ui->turretSlider->value());
}

void Kurk::closeEvent(QCloseEvent *event)
{
    if(bot.isRunning())
    {
        bot.killChildren();
        while(!bot.isFinished())
        {
            bot.terminate();
        }
        bot.wait();
    }
    event->accept();
}

void Kurk::on_btnStop_clicked()
{
    if(bot.isRunning())
    {
        bot.killChildren();
        while(!bot.isFinished())
        {
            bot.terminate();
        }
        bot.wait();
    }
    arduino.move('S');
}

void Kurk::on_btnStart_clicked()
{
    bot.connectBot(&arduino);
    bot.start();
}

void Kurk::on_btnGetVals_clicked()
{
    ui->txtLeftFor->setText(QString::number(arduino.getVals('L', 'F')));
    ui->txtLeftRev->setText(QString::number(arduino.getVals('L', 'R')));
    ui->txtLeftStop->setText(QString::number(arduino.getVals('L', 'S')));
    ui->txtRightFor->setText(QString::number(arduino.getVals('R', 'F')));
    ui->txtRightRev->setText(QString::number(arduino.getVals('R', 'R')));
    ui->txtRightStop->setText(QString::number(arduino.getVals('R', 'S')));
}

void Kurk::on_btnSetVals_clicked()
{
    arduino.setVals('L', 'F', ui->txtLeftFor->text().toInt());
    arduino.setVals('L', 'R', ui->txtLeftRev->text().toInt());
    arduino.setVals('L', 'S', ui->txtLeftStop->text().toInt());
    arduino.setVals('R', 'F', ui->txtRightFor->text().toInt());
    arduino.setVals('R', 'R', ui->txtRightRev->text().toInt());
    arduino.setVals('R', 'S', ui->txtRightStop->text().toInt());
}

void Kurk::on_btnSaveVals_clicked()
{
    QFile data("data.dat");
    if (data.open(QFile::WriteOnly | QFile::Truncate))
    {
        QTextStream in(&data);
        in << ui->txtLeftFor->text() << "\n";
        in << ui->txtLeftRev->text() << "\n";
        in << ui->txtLeftStop->text() << "\n";
        in << ui->txtRightFor->text() << "\n";
        in << ui->txtRightRev->text() << "\n";
        in << ui->txtRightStop->text() << "\n";
    }

    data.close();
}

void Kurk::on_btnLoadVals_clicked()
{
    QString leftFor, leftRev, leftStop, rightFor, rightRev, rightStop;
    QFile data("data.dat");
    if (data.open(QFile::ReadOnly))
    {
        QTextStream in(&data);
        if((leftFor = in.readLine()) != NULL)
            ui->txtLeftFor->setText(leftFor);
        else
            return;
        if((leftRev = in.readLine()) != NULL)
            ui->txtLeftRev->setText(leftRev);
        else
            return;
        if((leftStop = in.readLine()) != NULL)
            ui->txtLeftStop->setText(leftStop);
        else
            return;
        if((rightFor = in.readLine()) != NULL)
            ui->txtRightFor->setText(rightFor);
        else
            return;
        if((rightRev = in.readLine()) != NULL)
            ui->txtRightRev->setText(rightRev);
        else
            return;
        if((rightStop = in.readLine()) != NULL)
            ui->txtRightStop->setText(rightStop);
        else
            return;
    }

    data.close();
}
